// pages/taskdetail/taskdetail.js  待接单
const utils = require('../../utils/util');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: "",
        list: {},
        taskId: '',
        isShow: false,
        showPopup: false,
        agencyName: null,
        name: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.taskId) {
            this.setData({
                taskId: options.taskId
            });
        }
        this.getAgreementList();
        wx.showLoading({
            title: '正在加载中',
            duration: 1000
        });
        utils.FetchRequest(
            '/v1/api/task/detail?taskId=' + this.data.taskId,
            {}, 'get').then((res)=>{
            if (res.data.status == 200) {
                this.setData({
                    list: res.data.data,
                    url: res.data.data.agreementUrl,
                    isShow: true
                });
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
                wx.hideLoading();
                this.setData({
                    isShow: true
                });
            }
        });
        if (options.ok == 1) {
            utils.FetchRequest(
                '/v1/api/task/take?taskId=' + this.data.taskId,
                {}, 'get').then((res)=>{
                if (res.data.status == 200) {
                    wx.showToast({
                        title: '接单成功!',
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(()=>{
                        wx.switchTab({
                            url: "/pages/index/index",
                            success() {
                                app.globalData.status = "W";
                                var page = getCurrentPages().pop();
                                if (page == undefined || page == null) return;
                                if (typeof page.onshow === "function") {
                                    page.onshow();
                                    return;
                                }
                                page.onShow();
                            }
                        })
                    },2000)
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
            });
        }
    },
    showPop: function () {
        this.setData({
            showPopup: false
        });
        wx.navigateTo({
            url: "/pages/confirmationAgreement/confirmationAgreement"
        });
    },
    hidePop: function () {
        this.setData({
            showPopup: false
        })
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    getAgreementList: function () {
        wx.showLoading({
            title: '正在加载中！',
        });
        utils.FetchRequest('/v1/api/task/frameAgreementByCkerId', {}, "get").then((res) => {
            if (res.data.data.length != 0) {
                this.setData({
                    showPopup: true,
                    agencyName: res.data.data[0].agencyName,
                    name: res.data.data[0].name,
                });
                wx.hideLoading();
                wx.setStorageSync('agreementUrl', res.data.data);
            }
        });
    },
    viewContract: function () {
        if (this.data.url) {
            wx.navigateTo({
                url: "/pages/showPdf/showPdf?url=" + this.data.url
            })
        }
    },
    submit: function () {
        wx.showLoading({
            title: '正在加载中！',
        });
        utils.FetchRequest('/v1/api/task/agreement?taskId=' + this.data.taskId,{}, 'get').then((res)=>{
            if (res.data.status == 200) {
                if (res.data.data.length != 0) {
                    wx.setStorageSync('agreementUrl', res.data.data);
                    wx.navigateTo({
                        url: "/pages/confirmationAgreement/confirmationAgreement"
                    });
                } else {
                    utils.FetchRequest(
                        '/v1/api/task/take?taskId=' + this.data.taskId,
                        {}, 'get').then((res)=>{
                        if (res.data.status == 200) {
                            wx.showToast({
                                title: '接单成功!',
                                icon: 'none',
                                duration: 2000
                            });
                            setTimeout(()=>{
                                wx.switchTab({
                                    url: "/pages/index/index",
                                    success() {
                                        app.globalData.status = "W";
                                        var page = getCurrentPages().pop();
                                        if (page == undefined || page == null) return;
                                        if (typeof page.onshow === "function") {
                                            page.onshow();
                                            return;
                                        }
                                        page.onShow();
                                    }
                                })
                            },2000)
                        } else {
                            wx.showToast({
                                title: res.data.message,
                                icon: 'none',
                                duration: 2000
                            });
                        }
                    });
                }
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})